package servlet;

import net.sf.json.JSONArray;
import test.CountAvgBySql;
import test.SqlReturn;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author IlseTian
 * @ 2020-07-01- 20:51
 **/
@WebServlet(name = "ScoreServlet", value = "/ScoreServlet")
public class ScoreServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码
        response.setContentType("text/html;charset=utf-8");
        //请求解决乱码
        request.setCharacterEncoding("utf-8");
        //响应解决乱码
        response.setCharacterEncoding("utf-8");
//        第一个图：
        CountAvgBySql.main(new String[]{});
        Map<String, String> result = CountAvgBySql.countBySex();
        result = new HashMap<String, String>(result);
        //用户返回值给前端做柱形图
        SqlReturn bean = new SqlReturn();
        int i = 0;
        String[] moviesGenres = new String[18];//电影类型
        Double[] rateM = new Double[18];//女生对某类型电影打分的平均分
        Double[] rateF = new Double[18];//男生对某类型电影打分的平均分
        for (Map.Entry<String, String> entry : result.entrySet()) {
            //避免重复，value值是used意味着已经被合并过了
            if (!entry.getValue().equals("used")) {
                String[] mapKey = entry.getKey().split("[+]");
                if (mapKey[1].equals("F")) {
                    moviesGenres[i] = mapKey[0];
                    rateF[i] = Double.parseDouble(entry.getValue());
                    if (result.get(mapKey[0] + "+M") != null) {
                        rateM[i] = Double.parseDouble(result.get(mapKey[0] + "+M"));
                        //删除map中的值
                        result.put(mapKey[0] + "+M", "used");
                    } else
                        rateM[i] = 0.0;
                    result.put(mapKey[0] + "+F", "used");
                    i++;
                }
                if (mapKey[1].equals("M")) {
                    moviesGenres[i] = mapKey[0];
                    rateM[i] = Double.parseDouble(entry.getValue());
                    if (result.get(mapKey[0] + "+F") != null) {
                        rateF[i] = Double.parseDouble(result.get(mapKey[0] + "+F"));
                        //删除map中的值
                        result.put(mapKey[0] + "+F", "used");
                    } else
                        rateF[i] = 0.0;
                    result.put(mapKey[0] + "+M", "used");
                    i++;
                }
            }
        }
//        String[] typeData = {"Adventure", "Mystery", "Film-Noir", "Crime", "Musical",
//                "Western", "Children's", "Romance", "Animation", "Sci-Fi",
//                "Horror", "Action", "Drama", "Documentary", "Thriller", "War", "Comedy", "Fantasy"};
//        Double[] Frate = {3.58, 3.63, 3.8, 3.64, 4.03, 3.55, 3.82, 3.64, 4.09, 3.61, 3.22, 3.48, 3.8, 3.46, 3.56, 3.93, 3.55, 3.74};
//        Double[] Mrate = {3.58, 3.6, 4.1, 3.77, 3.62, 3.53, 3.47, 3.63, 3.81, 3.62, 3.47, 3.61, 3.81, 3.87, 3.62, 3.94, 3.52, 3.43};

        List<HashMap> list1 = new ArrayList<>();

        HashMap<String, String[]> map0 = new HashMap<String, String[]>();
        map0.put("typeData", moviesGenres);
        list1.add(map0);

        HashMap<String, Double[]> map1 = new HashMap<String, Double[]>();
        map1.put("maleData", rateM);
        map1.put("femaleData", rateF);
        list1.add(map1);


        //第二个图的数据：
        HashMap<String, Double[]> map2 = new HashMap<String, Double[]>();
        Double[] academic = {3.78, 3.77, 3.85, 3.77, 3.73};
        Double[] artist = {3.67, 3.66, 4.33, 3.94, 3.92};
        Double[] clerical = {3.63, 3.54, 4.29, 3.79, 3.6};
        Double[] college = {3.31, 3.24, 3.66, 3.16, 3.25};
        Double[] customer = {4.0, 3.67, 0.0, 0.0, 3.85};
        Double[] executive = {3.49, 3.48, 3.35, 3.04, 3.21};
        Double[] farmer = {3.79, 3.81, 4.0, 3.82, 3.75};
        Double[] homemaker = {3.75, 3.43, 4.25, 3.95, 3.84};
        Double[] K12 = {3.66, 3.69, 3.91, 3.86, 3.89};
        Double[] lawyer = {3.57, 3.67, 2.67, 2.83, 4.2};
        Double[] other = {3.68, 3.64, 3.95, 3.61, 3.81};
        Double[] programmer = {4.02, 3.93, 3.95, 3.83, 3.93};
        Double[] sales = {4.18, 3.75, 0.0, 0.0, 3.75};
        Double[] scientist = {2.95, 3.32, 3.64, 3.32, 3.29};
        Double[] selfEmployed = {3.43, 3.55, 2.95, 2.74, 3.01};
        Double[] technician = {3.56, 3.61, 4.32, 4.14, 3.75};
        Double[] tradesman = {4.43, 3.5, 2.0, 1.75, 3.64};
        Double[] unemployed = {2.71, 2.58, 3.5, 2.71, 2.85};
        Double[] writerData = {2.95, 3.0, 4.56, 4.42, 3.58};
        map2.put("academic", academic);
        map2.put("artist", artist);
        map2.put("clerical", clerical);
        map2.put("college", college);
        map2.put("customer", customer);
        map2.put("executive", executive);
        map2.put("farmer", farmer);
        map2.put("homemaker", homemaker);
        map2.put("K12", K12);
        map2.put("lawyer", lawyer);
        map2.put("other", other);
        map2.put("programmer", programmer);
        map2.put("sales", sales);
        map2.put("scientist", scientist);
        map2.put("selfEmployed", selfEmployed);
        map2.put("technician", technician);
        map2.put("tradesman", tradesman);
        map2.put("unemployed", unemployed);
        map2.put("writer", writerData);
        list1.add(map2);

        HashMap<String, String[]> map3 = new HashMap<String, String[]>();
        String[] moviesType = {"Action", "Adventure", "Animation", "Children's", "Comedy"};
        map3.put("moviesType", moviesType);
        list1.add(map3);

        PrintWriter writer = response.getWriter();
        JSONArray json = JSONArray.fromObject(list1);
        writer.println(json);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
